import { Body, Controller, Post } from '@midwayjs/core';
import { InjectEntityModel } from '@midwayjs/typeorm';
import { Repository } from 'typeorm';
import { requestBody } from '../interface/layout';
import { Admin, AdminEditDto } from '../entity/adminUser.entity';

@Controller('/adminuser')
export class AdminController {
  @InjectEntityModel(Admin)
  repoAdmin: Repository<Admin>;
  @Post('/edit')
  async reade(@Body() adminBody: AdminEditDto) {
    const admin = await this.repoAdmin.findOne({
      where: {
        adminId: adminBody.adminId,
      },
    });
    if (!admin) {
      return <requestBody>{
        code: -1,
        msg: '管理员信息不存在',
        data: null,
      };
    }
    admin.name = adminBody.name;
    admin.profile_picture = adminBody.profile_picture;
    const result = await this.repoAdmin.save(admin);
    delete result.password;
    return <requestBody>{
      code: 0,
      msg: '修改成功',
      data: result,
    };
  }
}
